package com.vueblog.service.impl;

import com.vueblog.dao.UserDao;
import com.vueblog.po.User;
import com.vueblog.service.UserService;
import com.vueblog.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUserById(int id) {
        return userDao.findUserById(id);
    }

    @Override
    public User findByUsernameAndPwd(User user) {
        return userDao.findByUsernameAndPwd(user);
    }

    @Override
    public User getAdminInfo() {
        return userDao.getAdminUser();
    }

    @Override
    public int updateAdminUser(User user) {
        user.setUpdateTime(new Date());
        return userDao.updateAdminUser(user);
    }

    @Override
    @Transactional
    public User getShowAboutInfo() {
        User user = userDao.getAdminUser();
        user.setPassword(null);
        user.setEmail(null);
        user.setUsername(null);
        user.setCreateTime(null);
        String content = user.getContent();
        content = MarkdownUtils.markdownToHtmlExtensions(content);
        user.setContent(content);
        userDao.updateAdminViews(user.getId());
        return user;
    }
}
